<?php

namespace app\admin\command;

use app\admin\model\wanlshop\Goods;
use app\api\service\wanlshop\Common;
use app\api\service\wanlshop\GoodsSku;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Log;

class Price extends Command
{
    protected function configure()
    {
        $this->setName('price')
            ->addOption('p', null, Option::VALUE_OPTIONAL, '页码', 1)
            ->setDescription('sync goods list data');
    }

    protected function execute(Input $input, Output $output)
    {
        $multi = \app\api\service\wanlshop\Goods::getBuybackMultiple();
        $multi = 2.66;
        date_default_timezone_set('PRC');
        ini_set('memory_limit', '-1');
        set_time_limit(0);
        $p = $input->getOption('p');
        /*Goods::where('status', 'normal')->chunk(1000, function ($goods) use ($output) {
            foreach ($goods as $item) {
                $st = microtime(true);
                $res = Common::getData('getGoodsDetails', ['father_id' => $item['c_father_goods_id']]);
                if ($res) {
                    $s = strtotime($res['c_start_datetime']);
                    $e = strtotime($res['c_stop_datetime']);
                    $g = Goods::find($item['id']);
                    $g->c_start_datetime = $res['c_start_datetime'];
                    $g->c_stop_datetime = $res['c_stop_datetime'];
                    $g->status = ($res['c_is_show'] != 1 || time() < $s || time() > $e) ? 'hidden' : 'normal';
                    $g->save();
                    $e = $g->status == 'hidden' ? '下架' : '';
                    $output->writeln("商品ID：{$item['id']} $e");
                }
            }
        });
        die();*/
        Goods::where('id', '>', 0)->where('platform', 1)->chunk(10000, function ($lists) use ($output, $multi) {
            foreach ($lists as $item) {
                $good = Goods::find($item['id']);
                $before = $good->c_in_price;
                $after = bcmul($before, $multi, 2);
                $good->price = $after;
                $good->save();
                $output->writeln("商品ID：{$item['id']}，{$item['title']} 进价：$before ，卖价：$after");
            }
        });
        \app\api\model\wanlshop\GoodsSku::where('id', '>', 0)->chunk(10000, function ($lists) use ($output, $multi) {
            foreach ($lists as $item) {
                if ($item['wps_father_goods_id']) {
                    $good = \app\api\model\wanlshop\GoodsSku::find($item['id']);
                    $before = $good->c_in_price;
                    $after = bcmul($before, $multi, 2);
                    $good->price = $after;
                    $good->save();
                    $output->writeln("商品sku-ID：{$item['id']} 进价：$before ，卖价：$after");
                }
            }
        });
        die();
        $list = Goods::where('id', '>', $p)->field('id,c_father_goods_id')->limit(200000)->select();
        var_dump('get list');
        foreach ($list as $good) {
            $goodsSkus = Common::getData('getGoodsDetails', ['father_id' => $good->c_father_goods_id]);
            if (!empty($goodsSkus['goods_sku'])) {
                foreach ($goodsSkus['goods_sku'] as $key => $item) {
                    $starttime = microtime(true);
                    $e = $item['c_goods_color'] . ',' . $item['c_goods_size'];
                    //给客户的价格我们用吊牌价，若吊牌价大于进价的2.1倍，则划去，写进价的2.1倍价格
                    $price = $item['c_in_price'] * $multi;
                    $sku = \app\api\model\wanlshop\GoodsSku::where(['goods_id' => $good->id, 'difference' => $e])->find();
                    if (!$sku) {
                        continue;
                    }
                    /*$sku->data([
//                            'goods_id' => $good->id,
//                            'thumbnail' => $item['c_goods_image'],
//                            'difference' => $e,
                        'price' => $price,
                        'market_price' => $item['c_original_price'],
                        'stock' => $item['c_goods_stock_valid'],
//                            'weigh' => 0,
//                            'sn' => $item['c_goods_id'],
//                            'sales' => 0,
//                            'state' => 0,
                        'wps_goods_id' => $item['c_goods_id'] ?? '',
                        'wps_father_goods_id' => $item['c_father_goods_id'] ?? '',
                        'c_in_price' => $item['c_in_price'] ?? '',
                    ]);*/
                    $sku->price = $price;
                    $sku->market_price = $item['c_original_price'];
                    $sku->stock = $item['c_goods_stock_valid'];
                    $sku->wps_goods_id = $item['c_goods_id'];
                    $sku->wps_father_goods_id = $item['c_father_goods_id'];
                    $sku->c_in_price = $item['c_in_price'];
                    $sku->save();
                    $in_price = $item['c_in_price'];
                    $endtime = microtime(true);
                    $ex = number_format($endtime - $starttime, 3);
                    $msg = "商品SKU数据，id:｛ $good->id - $key ｝,进价：$in_price,卖价：$price ，耗时:$ex 秒";
                    $output->writeln($msg);
                }
            }
        }
    }
}